package top.kaoshanji.p1ch07a.listener;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import java.util.Date;

/**
 * 会话监听器
 * @author kaoshanji
 * ^_^
 * create time 2025/4/5 10:53
 */
public class CustomBindingListener implements HttpSessionBindingListener {

    // 保存将要在 log() 方法中使用的 ServletContext
    ServletContext context;

    // 构造器传参的..
    public CustomBindingListener(ServletContext context) {
        this.context = context;
    }

    // 绑定到监听器时触发
    @Override
    public void valueBound(HttpSessionBindingEvent event) {
        context.log("[" + new Date() + "] BOUND as " + event.getName() + " to " + event.getSession().getId());
    }

    // 从监听器释放时触发
    @Override
    public void valueUnbound(HttpSessionBindingEvent event) {
        context.log("[" + new Date() + "] UNBOUND as " + event.getName() + " from " + event.getSession().getId());
    }

}
